#ifndef __SLOTHTTPD_PARSER_HEADERS_PARSER_HPP__
#define __SLOTHTTPD_PARSER_HEADERS_PARSER_HPP__

#include <slothttpd/data_buffer.hpp>
#include <slothttpd/parser/result_type.hpp>

#include <utility>

#include <boost/function.hpp>

namespace slothttpd {
namespace parser {

class headers_parser
{
public:
    headers_parser();
    
    // Note: callback's first arg (name) always contains ':' after it
    // TODO: add whole line ?
    result_type parse(receive_buffer_list &list, boost::function<void(char_range_t, char_range_t)> callback);
    void reallocate_buffer(data_buffer &old, data_buffer &new_) noexcept;
    
private:
    static void reallocate_part_of_word(data_buffer &old, data_buffer &new_, char **ptr) noexcept;
    
    enum parser_state {
        // parsing header name in progess
        state_name_first_char,
        state_name_char,
        
        // parsing header value in progress
        state_value_spaces,
        state_value_char,
        
        // skipping new lines
        state_lf_1,
        state_cr_lf_2,
        state_lf_2_end,
        
        state_error
    };
    
    char         *name_start;
    char         *name_end;
    char         *value_start;
    char         *value_end;
    
    parser_state    state_;
};

} //namespace parser
} //namespace slothttpd


#endif //__SLOTHTTPD_PARSER_HEADERS_PARSER_HPP__
